﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Utilities;
using ReactiveUI.Fody.Helpers;
using S7.Net.Types;

namespace Common.ViewModels
{

    /// <summary>
    /// 通道(一个通道一条直线段,多通道请求 节点图)
    /// </summary>
   
    public class PassVM : BaseViewModel
    {
        [Reactive] public string Name { get; set; }

        /// <summary>
        /// 直线段(通道可能是直线段也可能是圆弧段,这里没有圆弧段直接不考虑圆弧段，省的浪费时间)
        /// </summary>
        [Reactive]
        public LineRigidBody LineRigidBody { get; set; }

        /// <summary>
        /// 起始位置
        /// </summary>
        [Reactive]
        public double StartPossition { get; set; }

        /// <summary>
        /// 轨道长度
        /// </summary>
        [Reactive]
        public double Length { get; set; }

        [Reactive] public double 池子起点 { get; set; }

        /// <summary>
        /// 站点(站点属于通道)
        /// </summary>
        public List<Station> Stations { get; set; } = new List<Station>();

        [Reactive]
        public TransformRatio TransformRatio { get; set; } = new TransformRatio();

        /// <summary>
        /// 将位置转换为3D坐标(通道自身有一个坐标)
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public Point3D ConvertPositionToPoint3D(double position)
        {
            Point3D point3D = new Point3D();
            point3D.X = this.LineRigidBody.PointStart.X + this.TransformRatio.RatioX * position;
            point3D.Y = this.LineRigidBody.PointStart.Y + this.TransformRatio.RatioY * position;
            point3D.Z = this.LineRigidBody.PointStart.Z + this.TransformRatio.RatioZ * position;
            return point3D;
        }


        /// <summary>
        /// X方向平移变换
        /// </summary>
        [Reactive] public TranslateCoff TranslateCoffX { get; set; } = new TranslateCoff();

        /// <summary>
        /// Y系数 Y方向平移变换
        /// </summary>
        [Reactive] public TranslateCoff TranslateCoffY { get; set; } = new TranslateCoff();

        /// <summary>
        /// Z系数 Z方向平移变换
        /// </summary>
        [Reactive] public TranslateCoff TranslateCoffzZ { get; set; } = new TranslateCoff();

        /// <summary>
        /// 轨道上的运动需要一个位置  得出的位置 为基础位置加上位置
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public virtual Point3D GetPoint3D(IPosition position, bool needBase = false)
        {
            if (position is 池子 池子)
            {
                return GetPoint3D(池子);
            }

            //这里直接乘就行了
            Point3D point3D = new Point3D();
            point3D.X = this.TransformRatio.RatioX * (position.Position - this.StartPossition);
            point3D.Y = this.TransformRatio.RatioY * (position.Position - this.StartPossition);
            point3D.Z = this.TransformRatio.RatioZ * (position.Position - this.StartPossition);
            if (needBase)
            {
                point3D.X = this.LineRigidBody.PointStartBase.X + this.TransformRatio.RatioX * (position.Position - this.StartPossition);
                point3D.Y = this.LineRigidBody.PointStartBase.Y + this.TransformRatio.RatioY * (position.Position - this.StartPossition);
                point3D.Z = this.LineRigidBody.PointStartBase.Z + this.TransformRatio.RatioZ * (position.Position - this.StartPossition);
            }

            return point3D;
        }

        /// <summary>
        /// 池子同属于摆渡
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public virtual Point3D GetPoint3D(池子 position)
        {
            //这里直接乘就行了
            Point3D point3D = new Point3D();

            if (position.Position <= 14)
            {
                //上层
                //double startPointX= this.LineRigidBody.PointStart.X + this.Length-
                if (position.Position % 2 != 0)
                {
                    //奇数
                    int times = (int)(position.Position / 2);
                    point3D.X = this.池子起点 + position.XLength / 2 + (position.XLength + ProductLine.Instance.小池子间隔) * times;
                    point3D.Y = this.LineRigidBody.PointStartBase.Y - this.LineRigidBody.YLength / 2 - position.YLength - ProductLine.Instance.小池子间隔 - position.YLength / 2;
                    point3D.Z = 300;
                    //point3D.X = this.池子起点+
                }
                else
                {
                    int times = (int)(position.Position / 2) - 1;
                    point3D.X = this.池子起点 + position.XLength / 2 + (position.XLength + ProductLine.Instance.小池子间隔) * times;
                    point3D.Y = this.LineRigidBody.PointStartBase.Y - this.LineRigidBody.YLength / 2 - position.YLength / 2;
                    point3D.Z = 300;
                }
            }
            else
            {
                if (position.Position % 2 != 0)
                {
                    var select = 28 - position.Position;
                    int times = (int)(select / 2);
                    //奇数
                    point3D.X = this.池子起点 + position.XLength / 2 + (position.XLength + ProductLine.Instance.小池子间隔) * times; ;
                    point3D.Y = this.LineRigidBody.PointStartBase.Y + this.LineRigidBody.YLength / 2 + position.YLength / 2;
                    point3D.Z = 300;
                    //point3D.X = this.池子起点+
                }
                else
                {
                    var select = 30 - position.Position;
                    int times = (int)(select / 2) - 1;
                    point3D.X = this.池子起点 + position.XLength / 2 + (position.XLength + ProductLine.Instance.小池子间隔) * times; ;
                    point3D.Y = this.LineRigidBody.PointStartBase.Y + this.LineRigidBody.YLength / 2 + position.YLength + ProductLine.Instance.小池子间隔 + position.YLength / 2;
                    point3D.Z = 300;
                }
            }
            //point3D.X = this.TransformRatio.RatioX * position.Position;
            //point3D.Y = this.TransformRatio.RatioY * position.Position;
            //point3D.Z = this.TransformRatio.RatioZ * position.Position;
            return point3D;
        }
    }





}
